import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '../views/Home'
import login from '../views/login'
import register from '../views/register'

// 解决vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
 
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path:'/login',
    name:'login',
    component:login
  },
  {
    path:'/register',
    name:'register',
    component:register
  },
  {
    path:'/',
    name:'home',
    redirect:'/list/1',
    component:home,
    children:[
      {
        path:'/list/:id',
        name:'list',
        component:()=>import('../views/list')
      },
      {
        path:'list/:id/:pageid',
        name:'detail',
        component:()=>import('../views/detail')
      },
    ]
  },
  {
    path:'/personal',
    name:'personal',
    component:()=>import('../views/personal')
  }
]

const router = new VueRouter({
  routes
})

// 切换路由，页面返回顶部
router.afterEach((to,from)=>{
  window.scrollTo(0,0)
})
// router.beforeEach((to,from,next)=>{
//   var data=localStorage.getItem('user');
//   data=data?JSON.parse(data):''
//   if(!data) return 
//   console.log(data)
//   next()
// })
export default router
